package cn.alise.controller.admin;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.alise.controller.common.BaseController;
import cn.alise.qdp.model.common.MenuTree;
import cn.alise.qdp.model.menu.Menu;
import cn.alise.qdp.service.admin.MenuService;


/**
 * 菜单管理控制器
 * 
 */
@Controller
@RequestMapping(value = "/menu")
public class MenuController extends BaseController {

	private static final long serialVersionUID = 3071751434812887748L;
	private static final String ERROR_TITLE = "_error";
	private static final String REDIRECT_LIST = "redirect:list";

	@Resource(name = "menuService")
	private MenuService menuService;

	/**
	 * 获取菜单列表
	 */
	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request, Model model,
			Menu menu) {
		
		// 获取一级菜单
		List<Menu> menu1 = this.menuService.getOneList(menu);
		List<Menu> menuNew = new ArrayList<Menu>();
		for(int i=0; i<menu1.size(); i++) {
			menu1.get(i).setLevel(Menu.LEVEL_0);//设置级数 0一级菜单
			menuNew.add(menu1.get(i));
			//获取二级菜单
			menu.setParentId(menu1.get(i).getMenuId());
			List<Menu> menu2 = this.menuService.getTwoList(menu);
			for(int j=0; j<menu2.size(); j++) {
				menu2.get(j).setLevel(Menu.LEVEL_1);//设置级数 1二级菜单
				menuNew.add(menu2.get(j));
				//获取三级菜单
				menu.setParentId(menu2.get(j).getMenuId());
				List<Menu> menu3 = this.menuService.getButtonList(menu);
				for(int k=0; k<menu3.size(); k++) {
					menu3.get(k).setLevel(Menu.LEVEL_2);//设置级数 2三级按钮
					menuNew.add(menu3.get(k));
				}
			}
		}
		model.addAttribute("list", menuNew);

		return "pages/menu/menu-list";
	}
	
	/**
	 * 跳转到菜单创建页面
	 */
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(HttpServletRequest request, Model model) {
		
		return "pages/menu/menu-new";
	}
	
	/**
	 * 获取所有一、二级菜单
	 */
	@RequestMapping(value = "/findAllTree")
	@ResponseBody
	public List<MenuTree> findAllTree() {
		
		// 获取一级菜单
		Menu menu = new Menu();
		List<Menu> menu1 = this.menuService.getOneList(menu);
		List<MenuTree> menuTree = new ArrayList<MenuTree>();
		for(int i=0; i<menu1.size(); i++) {
			MenuTree treeOne = new MenuTree();
			treeOne.setId(menu1.get(i).getMenuId());
			treeOne.setText(menu1.get(i).getMenuName());
			if(menu1.get(i).getParentId()!=null) {
				treeOne.setPid(menu1.get(i).getParentId().toString());
			} else {
				treeOne.setPid("");
			}
			treeOne.setAttributes(menu1.get(i).getDataHref());
			
			//获取二级菜单
			menu.setParentId(menu1.get(i).getMenuId());
			List<Menu> menu2 = this.menuService.getTwoList(menu);
			if (menu2 != null) {
				List<MenuTree> tree = new ArrayList<MenuTree>();
				for(int j=0; j<menu2.size(); j++) {
					MenuTree treeTwo = new MenuTree();
					treeTwo.setId(menu2.get(j).getMenuId());
					treeTwo.setText(menu2.get(j).getMenuName());
					if(menu2.get(j).getParentId()!=null) {
						treeTwo.setPid(menu2.get(j).getParentId().toString());
					} else {
						treeTwo.setPid("");
					}
					treeTwo.setAttributes(menu2.get(j).getDataHref());
					tree.add(treeTwo);
				}
				treeOne.setChildren(tree);
			} else {
                treeOne.setState("closed");
            } 
			menuTree.add(treeOne);
		}
		return menuTree;
	}
	
	/**
	 * 获取所有一、二、三级菜单
	 */
	@RequestMapping(value = "/findAllTrees")
	@ResponseBody
	public List<MenuTree> findAllTrees() {
		
		// 获取一级菜单
		Menu menu = new Menu();
		List<Menu> menu1 = this.menuService.getOneList(menu);
		List<MenuTree> menuTree = new ArrayList<MenuTree>();
		for(int i=0; i<menu1.size(); i++) {
			MenuTree treeOne = new MenuTree();
			treeOne.setId(menu1.get(i).getMenuId());
			treeOne.setText(menu1.get(i).getMenuName());
			if(menu1.get(i).getParentId()!=null) {
				treeOne.setPid(menu1.get(i).getParentId().toString());
			} else {
				treeOne.setPid("");
			}
			treeOne.setAttributes(menu1.get(i).getDataHref());
			
			//获取二级菜单
			menu.setParentId(menu1.get(i).getMenuId());
			List<Menu> menu2 = this.menuService.getTwoList(menu);
			if (menu2 != null) {
				List<MenuTree> tree = new ArrayList<MenuTree>();
				for(int j=0; j<menu2.size(); j++) {
					MenuTree treeTwo = new MenuTree();
					treeTwo.setId(menu2.get(j).getMenuId());
					treeTwo.setText(menu2.get(j).getMenuName());
					if(menu2.get(j).getParentId()!=null) {
						treeTwo.setPid(menu2.get(j).getParentId().toString());
					} else {
						treeTwo.setPid("");
					}
					
					//获取三级菜单
					menu.setParentId(menu2.get(j).getMenuId());
					List<Menu> menu3 = this.menuService.getButtonList(menu);
					if(menu3 != null) {
						List<MenuTree> buttonTree = new ArrayList<MenuTree>();
						for(int k=0; k<menu3.size(); k++) {
							MenuTree treeThree = new MenuTree();
							treeThree.setId(menu3.get(k).getMenuId());
							treeThree.setText(menu3.get(k).getMenuName());
							if(menu3.get(k).getParentId()!=null) {
								treeThree.setPid(menu3.get(k).getParentId().toString());
							} else {
								treeThree.setPid("");
							}
							treeThree.setAttributes(menu3.get(k).getDataHref());
							buttonTree.add(treeThree);
						}
						treeTwo.setChildren(buttonTree);
					} else {
						treeTwo.setState("closed");
					}
					treeTwo.setAttributes(menu2.get(j).getDataHref());
					tree.add(treeTwo);
				}
				treeOne.setChildren(tree);
			} else {
                treeOne.setState("closed");
            } 
			menuTree.add(treeOne);
		}
		return menuTree;
	}
	
	/**
	 * 新增菜单
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public String insert(HttpServletRequest request, Menu menu, RedirectAttributes attr) {
		
		if(menu.getParentId()!=null && menu.getParentId()!=0) {
			menu.setParentId(menu.getParentId());//上级菜单Id
		} else {
			menu.setParentId(null);//上级菜单Id
		}
		if(menu.getParentId()!=null&&!"".equals(menu.getParentId())&&menu.getType()==1) {
			menu.setDataId(menu.getDataId());//dataId
		} else {
			menu.setDataId("");//dataId
		}
		if(menu.getParentId()!=null&&!"".equals(menu.getParentId())&&menu.getType()==1) {
			menu.setIconPath("");//图标路径
		} else {
			menu.setIconPath(menu.getIconPath());//图标路径
		}
		menu.setCreatedTime(new Timestamp(new Date().getTime()));
		
		int row = this.menuService.save(menu);
		
		if (row >= 0) {
			attr.addFlashAttribute(ERROR_TITLE, "菜单新增成功！");
		} else {
			attr.addFlashAttribute(ERROR_TITLE, "菜单新增失败！");
		}
		return REDIRECT_LIST;
	}
	
	/**
	 * 跳转到菜单编辑页面
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(HttpServletRequest request, Model model, Menu menu) {
		
		// 获取修改的菜单信息
		menu = this.menuService.get(menu);
		model.addAttribute("menu", menu);
		 
		return "pages/menu/menu-edit";
	}
	
	/**
	 * 修改菜单
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(HttpServletRequest request, Model model, Menu menu, RedirectAttributes attr) {
		
		if(menu.getParentId()!=null && menu.getParentId()!=0) {
			menu.setParentId(menu.getParentId());//上级菜单Id
		} else {
			menu.setParentId(null);//上级菜单Id
		}
		
		// 调用接口执行更新
		menu.setUpdatedTime(new Timestamp(new Date().getTime()));
		int row = this.menuService.update(menu);
		
		if (row >= 0) {
			attr.addFlashAttribute(ERROR_TITLE, "菜单修改成功！");
		} else {
			attr.addFlashAttribute(ERROR_TITLE, "菜单修改失败！");
		}
		return REDIRECT_LIST;
	}
}